package com.bnz.manager.service.impl;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.manager.dao.ContentDao;
import com.bnz.manager.entity.ContentEntity;
import com.bnz.manager.service.ContentService;


@Service("contentService")
public class ContentServiceImpl extends ServiceImpl<ContentDao, ContentEntity> implements ContentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ContentEntity> page = this.page(
                new Query<ContentEntity>().getPage(params),
                new QueryWrapper<ContentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    //1.只删除指定(空间:key)的数据
//    @CacheEvict(value = "contents",key = "'findAll'")
    //2.删除所有key的数据,但是要用value指定命名空间名,省略会报错!
//    @CacheEvict(value = "contents",allEntries = true)
    public void add(ContentEntity content) {
        System.out.println("正在清除缓存数据..................");
        this.save(content);
    }

    @Override
    //此注解的方法每次都被调用
//    @CachePut(value = "content",key = "#content.id")
    @Caching( put = {@CachePut(value = "content",key = "#content.id")},
              evict = {@CacheEvict(value = "contents",key = "'findAll'")})
    public ContentEntity updateContent(ContentEntity content) {
        System.out.println("正在更新缓存...................");
        this.updateById(content);
        return content;
    }

    @Override
    @Cacheable(value = "content",key = "#id")
    public ContentEntity findById(String id) {
        System.out.println("正在查询数据库,更新缓存.......");
        return this.getById(id);
    }

}